package com.schedule;

import com.constant.RedisKey;
import com.dao.ActivityDao;
import com.dao.RecordDao;
import com.dao.ScheduleDao;
import com.service.ActivityServiceImpl;
import com.vo.ActivityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @author linyukun
 * @date 2022/10/9 15:30
 * 定时任务
 */
@Configuration
@EnableScheduling
public class ScheduleServiceImpl {
    @Autowired
    private ActivityDao activityDao;

    @Autowired
    private RecordDao recordDao;

    @Autowired
    private ScheduleDao scheduleDao;

    @Scheduled(cron = "0 0/1 * * * ?")//
    public void brandDataUpdate(){
        try {
            System.out.println("每一分钟");
            //region 活动的处理
            List<ActivityVO> activitys = activityDao.getAllNoFinishActivitys();
            if(!CollectionUtils.isEmpty(activitys)){
                long now = new Date().getTime();
                for (ActivityVO activity : activitys) {
                    boolean isChangState = false;
                    if(!ObjectUtils.isEmpty(activity.getSignStartTime())){
                        long signStartTime = activity.getSignStartTime().getTime();
                        long signEndTime = activity.getSignEndTime().getTime();
                        if(signStartTime <= now && now <= signEndTime){//签到时间
                            isChangState = true;
                            activityDao.updateActivityState(activity.getId(),2);//签到中
                        }
                    }
                    //活动开始时间
                    if( !isChangState && !ObjectUtils.isEmpty(activity.getActivityStartTime())){
                         long activityStartTime = activity.getActivityStartTime().getTime();
                         long activityEndTime = activity.getActivityEndTime().getTime();
                         if(activityStartTime <= now && now <= activityEndTime){//活动开始时间和结束时间
                             activityDao.updateActivityState(activity.getId(),3);//进行中
                         }else if(activityEndTime <= now){
                             activityDao.updateActivityState(activity.getId(),5);//结束
                         }
                     }
                }
            }
            //endregion 活动的处理

            //region 判断哪些借阅的人没有还书
            recordDao.updateNoBackRecordInfo();
            //endregion 判断哪些借阅的人没有还书

            //region 修改图书库存的状态(是否可以借书）
            scheduleDao.updateBookStockStatus();
            //endregion 判断哪些借阅的人没有还书


            //region 书籍类型的所属关系的判断
            scheduleDao.updateTypeBelongType();
            //endregion 书籍类型的所属关系的判断


        }catch (Exception e){
            System.out.println("报错了");
            System.out.println(e.getMessage());
        }
    }
}
